package com.ir.stat.spark.utils;

import com.ir.stat.spark.bean.ResourceDirStat;
import com.ir.stat.spark.service.ResourceStatService;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;

import java.io.File;
import java.io.IOException;

public class HdfsUtil {

    private static HdfsUtil hdfsUtil = new HdfsUtil();

    public static HdfsUtil getInstance() {
        return hdfsUtil;
    }

    private FileSystem fs;

    private HdfsUtil() {
        init();
    }

    private void init() {
        try {
            Configuration conf = new Configuration();
            String USER_NAME = System.getenv("HADOOP_USER_NAME");
            if (StringUtils.isBlank(USER_NAME)) {
                USER_NAME = "merce";
            }
            System.setProperty("HADOOP_USER_NAME", USER_NAME);
            String CONF_PATH = System.getenv("HADOOP_CONF_DIR");
            if (StringUtils.isNotBlank(CONF_PATH)) {
                conf.addResource(new Path(CONF_PATH + File.separator + "core-site.xml"));
                conf.addResource(new Path(CONF_PATH + File.separator + "core-site.xml"));
            }
            conf.setBoolean("fs.hdfs.impl.disable.cache", true);
            this.fs = FileSystem.get(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean clean(String filePath) {
        try {
            Path hdfsPath = new Path(filePath);
            if (fs.exists(hdfsPath)) {
                boolean isDeleted = fs.delete(hdfsPath, true);
                if (isDeleted) {
                    return fs.mkdirs(hdfsPath);
                }
            }

        } catch (IOException e) {
            init();
        }
        return false;
    }

    public boolean exist(String path) {
        try {
            return fs.exists(new Path(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public FileStatus status(String path) {
        if (!exist(path)) return null;
        try {
            FileStatus status = fs.getFileStatus(new Path(path));
            return status;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    public FileStatus[] listStatus(String path) {
        if (!exist(path)) return null;
        try {
            return fs.listStatus(new Path(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new FileStatus[0];
    }

    public RemoteIterator<LocatedFileStatus> list(String path, boolean recursive) throws Throwable {
        if (!exist(path)) {
            return null;
        }
        Path p = new Path(path);
        RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(p, recursive);
        return iterator;
    }

    public static void main(String[] args) {

        String path = "/tmp/test/test_transform_cast_sink_1";


        ResourceStatService service = new ResourceStatService();

        ResourceDirStat root = new ResourceDirStat();
        root.setId(MD5.get(path));
        root.setName(path);

        try {
            service.countFiles(root);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        System.out.println(JsonBuilder.getInstance().toJson(root));


//        RemoteIterator<LocatedFileStatus> iterator = null;
//
//        try {
//            iterator = getInstance().list(path, true);
//            long tSize = 0l;
//            int tCount = 0;
//            while (iterator.hasNext()) {
//                LocatedFileStatus fileStatus = iterator.next();
//                tSize += fileStatus.getLen();
//                tCount++;
//            }
//            System.out.println("tSize=" + tSize + ", tCount=" + tCount);
//        } catch (Throwable throwable) {
//            throwable.printStackTrace();
//        }


//        ResourceDirStat root = new ResourceDirStat();
//        root.setId(MD5.get(path));
//        root.setName(path);
//
//        try {
//            getInstance().countFiles(root);
//            System.out.println(JsonBuilder.getInstance().toJson(rds));
//        } catch (Throwable throwable) {
//            throwable.printStackTrace();
//        }


//        try {
//            FileStatus fs = getInstance().status("hdfs://info1:8020/tmp/ShotgunsAppMaster.jar");
//            System.out.println(fs.getPath().toUri().getPath());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        try {
//            RemoteIterator<LocatedFileStatus> iterator = getInstance().list("/tmp", false);
//            while(iterator.hasNext()){
//                System.out.println(iterator.next().getPath());
//            }
//        } catch (Throwable throwable) {
//            throwable.printStackTrace();
//        }
//        try {
//            boolean ret = getInstance().exist("/tmp/spc");
//            System.out.println(ret);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
}
